/**
 * Candidate number: 35834
 */
package tv.starfu.systems;

import com.artemis.Component;
import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.EntityProcessingSystem;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.UnsupportedAudioFileException;
import net.beadsproject.beads.core.AudioContext;
import net.beadsproject.beads.data.Sample;
import net.beadsproject.beads.data.SampleManager;
import net.beadsproject.beads.ugens.Envelope;
import net.beadsproject.beads.ugens.Gain;
import net.beadsproject.beads.ugens.SamplePlayer;
import tv.starfu.sequencer.components.AmpEnvelope;
import tv.starfu.sequencer.components.PitchEnvelope;
import tv.starfu.sequencer.components.SamplePlayback;
import tv.starfu.sequencer.components.TimeVector;

/**
 * process entities with given component types
 * @author nia radical
 */
public class SampleEventSystem extends EntityProcessingSystem {

    private TimeVector now;
    private AudioContext ac;
    private SampleManager sm;
    
    private ComponentMapper<TimeVector> tvMapper;
    private ComponentMapper<AmpEnvelope> ampEnvMapper;
    private ComponentMapper<PitchEnvelope> pitchEnvMapper;
    private ComponentMapper<SamplePlayback> sampleMapper;

    public SampleEventSystem(AudioContext ac, TimeVector now) {
        super(TimeVector.class, SamplePlayback.class, AmpEnvelope.class, PitchEnvelope.class);
        this.now = now;
        this.ac = ac;
    }
    
    @Override
    public void initialize() {
        // init componentMappers
         tvMapper = new ComponentMapper<TimeVector>(TimeVector.class, world);
         ampEnvMapper = new ComponentMapper<AmpEnvelope>(AmpEnvelope.class, world);
         pitchEnvMapper = new ComponentMapper<PitchEnvelope>(PitchEnvelope.class, world);
         sampleMapper = new ComponentMapper<SamplePlayback>(SamplePlayback.class, world);
         sm = new SampleManager();
         SampleManager.group("kit1", new Sample[] 
				{ 
                                  /* KICKS : [ 0 - 7 ] */
                                  SampleManager.sample("Assets/Sounds/drumsounds/b1.wav"),
				  SampleManager.sample("Assets/Sounds/drumsounds/b2.wav"),
				  SampleManager.sample("Assets/Sounds/drumsounds/b3.wav"),
				  SampleManager.sample("Assets/Sounds/drumsounds/b4.wav"),
				  SampleManager.sample("Assets/Sounds/drumsounds/b5.wav"),
				  SampleManager.sample("Assets/Sounds/drumsounds/b6.wav"),
				  SampleManager.sample("Assets/Sounds/drumsounds/b7.wav"),
				  SampleManager.sample("Assets/Sounds/drumsounds/b8.wav"),
                                  
                                  /* SNARE :[ 8 - 11] */
                                  SampleManager.sample("Assets/Sounds/drumsounds/s1.wav"),
				  SampleManager.sample("Assets/Sounds/drumsounds/s2.wav"),
				  SampleManager.sample("Assets/Sounds/drumsounds/s3.wav"),
				  SampleManager.sample("Assets/Sounds/drumsounds/s4.wav"),
                                  
                                  /* OPEN HAT [ 12 - 13] */
                                  SampleManager.sample("Assets/Sounds/drumsounds/oh1.wav"),
				  SampleManager.sample("Assets/Sounds/drumsounds/oh2.wav"),
                                 
                                  /* CLOSED HAT [ 14 - 15 ] */
				  SampleManager.sample("Assets/Sounds/drumsounds/ch1.wav"),
				  SampleManager.sample("Assets/Sounds/drumsounds/ch2.wav"),
                                  
                                });
    }
    
    @Override
    protected void process(Entity e) {
        // Get TV components from each Event entity
         TimeVector tv = tvMapper.get(e);
         AmpEnvelope ampEnv = ampEnvMapper.get(e);
         PitchEnvelope pitchEnv = pitchEnvMapper.get(e);
         SamplePlayback sample = sampleMapper.get(e);
         
       //  System.out.println(tv.toString());
         if(now.compareTo(tv) == 0) {
             System.out.println("Yeyo" + tv.compareTo(now));
             System.out.println("Tv: " + tv.toString());
             System.out.println("Now:" + now.toString());
             
             
             Envelope amp = ampEnv.getEnvelope();
             Gain gain = new Gain(ac, 1, amp);
             String sampleName = sample.getSampleGroup();
             
             SamplePlayer sp = new SamplePlayer(ac, SampleManager.fromGroup(sampleName, sample.getSampleIndex()));
             gain.addInput(sp);
             ac.out.addInput(gain);
         }
    
    }
        
    }

